package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.service.entity.pojo.ShapeDesc;
import com.spice.service.mapper.ShapeDescMapper;
import com.spice.service.service.ShapeDescService;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

@Service
public class ShapeDescServiceImpl extends ServiceImpl<ShapeDescMapper, ShapeDesc> implements ShapeDescService {

    public Response saveShapeDesc(ShapeDesc newShapeDesc) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(
                newShapeDesc.getName(),
                newShapeDesc.getShape(),
                newShapeDesc.getMaintainer())) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        } else {
            //检查名称是否重复？
            if (this.count(new LambdaQueryWrapper<ShapeDesc>().eq(ShapeDesc::getName, newShapeDesc.getName())) > 0) {
                throw new SpiceException().message("该品种已存在");
            }
            this.save(newShapeDesc);
            return Response.ok().message("添加外形描述数据成功").data("data", newShapeDesc);
        }
    }

    public Response removeShapeDesc(Long id) {
        // 确认必要参数
        if (CheckUtil.objectsHasNull(id)) {
            throw new SpiceException(ResultCodeEnum.PARAM_LACK);
        } else {
            //确认要删除的是否存在
            ShapeDesc removedShapeDesc = this.getById(id);
            if (CheckUtil.objectsHasNull(removedShapeDesc)) {
                throw new SpiceException().message("该辣椒外形描述数据不存在");
            }
            this.removeById(id);
            return Response.ok().message("辣椒外形描述数据删除成功").data("data", removedShapeDesc);
        }
    }

    public Response updateShapeDesc(Long id, ShapeDesc newShapeDesc) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(id, newShapeDesc)) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        } else {
            //确认待更新数据是否存在
            ShapeDesc updatedShapeDesc = this.getById(id);
            if (CheckUtil.objectsHasNull(updatedShapeDesc)) {
                throw new SpiceException().message("该辣椒外形描述数据不存在");
            }
            //确认更新后的辣椒名称是否与已有的重名
            //更新后的名称与已有的不同的情况下
            if (!updatedShapeDesc.getName().equals(newShapeDesc.getName())) {
                if (this.count(new LambdaQueryWrapper<ShapeDesc>().eq(ShapeDesc::getName,newShapeDesc.getName())) > 0) {
                    throw new SpiceException().message("该辣椒品种已存在");
                }
            }
            //更新待更新的数据id和createTime
            newShapeDesc.setId(id);
            newShapeDesc.setCreateTime(updatedShapeDesc.getCreateTime());
            if (this.updateById(newShapeDesc)) {
                return Response.ok().message("辣椒外形描述数据更新成功").data("data", newShapeDesc);
            } else {
                return Response.error().message("辣椒外形描述数据更新失败");
            }
        }
    }
}
